package com.webull.information.center.carwler.task.handler;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import com.webull.information.center.carwler.task.support.TikcerNewsProcessor;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.webull.information.center.carwler.common.model.NewsInformation;
import com.webull.information.center.carwler.common.model.StockConfig;
import com.webull.information.center.carwler.common.model.StockTitle;
import com.webull.information.center.carwler.common.util.jsoup.HttpJsoupClient;
import com.webull.information.center.carwler.common.util.news.MctrolNewsListUitl;
import com.webull.information.center.carwler.common.util.news.MctrolNewsListUitl.MctrolItem;
import com.webull.information.center.carwler.task.support.StockConfigSupport;
import com.webull.information.center.common.constants.Constants;
import com.webull.information.client.model.News;
import com.webull.information.client.model.parm.NewsRelType;

/**
 * 新浪新闻处理
 * 
 * @author shimingjun
 * @date 2016年8月26日 下午4:06:09
 * @version 1.0
 * @since JDK 1.8
 */
@Component
public class StockMctrolInNewsHandler {

	protected final Logger logger = LogManager.getLogger(getClass());
	private static String mctrol_lang = Constants.lang_en;// Mctrol语言版本
	private @Autowired MctrolNewsListUitl mctrolNewsListUitl;
	private @Autowired StockConfigSupport stockConfigSupport;
	private @Autowired
    TikcerNewsProcessor tikcerNewsProcessor;

	public void handlerByStockTitle4Mcontrol(StockTitle stockTitle) {
		StockConfig stockConfig = stockConfigSupport.getStockConfig(stockTitle, "mctrol", mctrol_lang);
		processMcontrolList(stockConfig, stockTitle);
	}

	private void processMcontrolList(StockConfig stockConfig, StockTitle stockTitle) {
		try {
			List<Date> times = new ArrayList<>();
			Date lastPush = stockConfig.getCollectTime();
			/**
			 * 获取标的物新闻首页列表
			 */
			List<MctrolItem> newsList = mctrolNewsListUitl.parseNewsList(stockTitle);
			//
			entrie: for (int i = 0; newsList != null && i < newsList.size(); i++) {
				try {
					MctrolItem item = newsList.get(i);
					Date push = item.getNewsTime();
					if (push == null) {
						logger.warn("-processSinaList stockTitle:{}, url:{} SinaItem:{}, push can't be parse!!",
								stockTitle, item);
						continue entrie;
					}

					if (lastPush == null || DateUtils.isSameDay(push, new Date()) || push.after(lastPush)) {
						times.add(push);
						processMctrolNews(stockTitle, item);
					}
				} catch (Exception e) {
					logger.warn(e);
					break entrie;
				}
			}

			if (!times.isEmpty()) {
				tikcerNewsProcessor.updateStockConfig(stockConfig,times);
				stockConfigSupport.updateById(stockConfig);
			}

		} catch (Exception e) {
			logger.error("processRss(Date lastPush, StockTitle stockTitle, String compFinanceRss)", e);
		}
	}

	private void processMctrolNews(StockTitle stockTitle, MctrolItem item) throws Exception {
		String title = item.getTitle();
		String link = item.getLink();
		NewsInformation info = new NewsInformation();// produce(doc);

		info.setTitle(title);
		info.setRegionId(stockTitle.getRegionId());
		info.setSummary(item.getSummy());
		info.addMainPic(item.getImg());
		info.setSourceName(Optional.ofNullable(StringUtils.stripToNull(item.getSoure())).orElse("moneycontrol.com"));

		info.setNewsTime(item.getNewsTime());
		info.setSiteType(News.SiteType.PIRORIT);

		info.setUrl(link);
		info.setLanguage(mctrol_lang);
		info.setCollectSource(News.CollectSource.mCtrol);
		String md5 = HttpJsoupClient.finishingMd5Url(link);
		info.setUrlMd5(md5);

		tikcerNewsProcessor.bindTargetNewsRecord(info, stockTitle.getTickerId(), NewsRelType.ticker);
	}

}
